package com.lala.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 霍明铭
 * @date 2020/3/29 13:09
 */
@Configuration
public class RabbitMqConfig {
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("order_exchange");
    }

    @Bean
    public Queue queue(){
        return new Queue("client_order_queue",true,false,false);
    }
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(topicExchange()).with("client.order.push");
    }

    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory, Jackson2JsonMessageConverter jackson2JsonMessageConverter){
        RabbitTemplate rabbitTemplate=new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(factory);
        //rabbitTemplate.setConfirmCallback(confirmCallback);
       // rabbitTemplate.setReturnCallback(returnCallback);
        rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
        //rabbitTemplate.setMandatory(true);
        return  rabbitTemplate;
    }


}
